package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.SupplierCriteria;
import com.leo.ibatis.demo.domain.Supplier;
import com.leo.ibatis.demo.service.SupplierService;

public class SupplierController extends MultiActionController {

	private SupplierService supplierService;

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SupplierCriteria supplierCriteria = new SupplierCriteria();
		bind(request, supplierCriteria);
		
		List<Supplier> supplierList = supplierService.listOnPage(supplierCriteria);
		
		ModelAndView mav = new ModelAndView("supplier/list");
		mav.addObject("supplierList", supplierList);
		mav.addObject("supplierCriteria", supplierCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SupplierCriteria supplierCriteria = new SupplierCriteria();
		bind(request, supplierCriteria);
			
		int suppid = ServletRequestUtils.getRequiredIntParameter(request, "suppid");

		Supplier supplier = supplierService.select(suppid);
		
		ModelAndView mav = new ModelAndView("supplier/detail");
		mav.addObject("supplier", supplier);
		mav.addObject("supplierCriteria", supplierCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int suppid = ServletRequestUtils.getRequiredIntParameter(request, "suppid");

		supplierService.delete(suppid);
		
		return new ModelAndView("redirect:supplier.do");
	}

}
